package com.hydra.wiki.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hydra.wiki.core.page.ViewMode;
import com.hydra.wiki.core.sql.query.DataQuery;
import com.hydra.wiki.doc.dao.FarmRfDoctypeDaoInter;
import com.hydra.wiki.doc.domain.FarmDoctype;
import com.hydra.wiki.doc.server.FarmDocRunInfoInter;
import com.hydra.wiki.doc.server.FarmDocTypeInter;
import com.hydra.wiki.web.WebUtils;
import com.hydra.wiki.web.easyui.EasyUiTreeNode;
import com.hydra.wiki.web.easyui.EasyUiUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RequestMapping("/frame")
@Controller
public class FrameController extends WebUtils {
    private final static Logger log = Logger.getLogger(FrameController.class);
    @Resource
    private FarmDocRunInfoInter farmDocRunInfoImpl;

    @Resource
    private FarmRfDoctypeDaoInter farmRfDoctypeDaoInter;

    @RequestMapping("/index")
    public ModelAndView index(HttpServletRequest request, HttpSession session) {
        return ViewMode.getInstance().putAttr("menus", getCurrentUserMenus(session)).returnModelAndView("frame/frame");
    }

    @RequestMapping("/home")
    public ModelAndView home(HttpServletRequest request, HttpSession session) {
        Map<String, Object> map = null;
        try {
            map = new HashMap<>();
            Map<String, Integer> statNum = farmDocRunInfoImpl.getStatNum();
            List list = farmRfDoctypeDaoInter.selectCount();
            List<String> keyList = new ArrayList<>();
            List<Long> valueList = new ArrayList<>();
            JSONArray array;
            for (Object o : list) {
                array = (JSONArray)JSON.toJSON(o);
                keyList.add(array.getString(0));
                valueList.add(array.getLongValue(1));
            }
            Map<String, Object> docTypeMap = new HashMap<>();
            docTypeMap.put("key", JSON.toJSONString(keyList));
            docTypeMap.put("value", valueList);
            map.put("STAT", statNum);
            map.put("DOCTYPE", docTypeMap);
        } catch (Exception e) {
            map = new HashMap<>();
            log.error(e.getMessage());
        }
        return ViewMode.getInstance().putAttr("result", map).returnModelAndView("frame/home");
    }

    @SuppressWarnings("unchecked")
    @RequestMapping("/service")
    @ResponseBody
    public List<EasyUiTreeNode> allUrl(DataQuery query, HttpServletRequest request) {
        log.info("正式系统请 关闭该服务");
        ServletContext servletContext = request.getSession().getServletContext();
        WebApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        // 获取所有的RequestMapping
        Map<String, Object> allRequestMappings = appContext.getBeansWithAnnotation(Controller.class);
        int n = 1;
        for (Object obj : allRequestMappings.values()) {
            RequestMapping classRequest = obj.getClass().getAnnotation(RequestMapping.class);
            if (classRequest != null) {
                // 封装父节点
                Map<String, Object> superNode = new HashMap<String, Object>();
                int m = ++n;
                superNode.put("SID", n);
                superNode.put("PID", "none");
                if (classRequest.value() == null || classRequest.value().length == 0) {
                    superNode.put("NA", "NoGroup");
                } else {
                    superNode.put("NA", classRequest.value()[0]);
                }
                list.add(superNode);
                for (Method method : obj.getClass().getMethods()) {
                    RequestMapping methodRequest = method.getAnnotation(RequestMapping.class);
                    if (methodRequest != null) {
                        // 封装子节点
                        Map<String, Object> childeNode = new HashMap<String, Object>();
                        n++;
                        childeNode.put("SID", n);
                        childeNode.put("PID", m);

                        if (classRequest.value() == null || classRequest.value().length == 0) {
                            childeNode.put("NA", methodRequest.value()[0].replaceAll("/", ""));
                        } else {
                            childeNode.put("NA",
                                    classRequest.value()[0].replaceAll("/", "") + methodRequest.value()[0]);
                        }

                        list.add(childeNode);
                    }
                }
            }
        }
        return (List<EasyUiTreeNode>) ViewMode.returnListObjMode(EasyUiUtils.formatAjaxTree(list, "PID", "SID", "NA"));
    }
}
